#!/usr/bin/env python
# A script to convert PostgreSQL's COPY TO text (tsv; tab-delimited values) lines into JSON sequence
# Usage: pgtsv_to_json COLUMN_NAME1:TYPE1 COLUMN_NAME2:TYPE2 ...

from ddlib.util import PGTSVParser
import json, sys, argparse
reload(sys).setdefaultencoding("utf8")

parser = argparse.ArgumentParser()
parser.add_argument('schema', nargs="+")
args = parser.parse_args()

name_type_pairs = [(nm,ty) for arg in args.schema for nm, _, ty in [arg.partition(':')]]
pgtsv_parser = PGTSVParser(name_type_pairs)

for obj in pgtsv_parser.parse_stdin():
  json.dump(obj._asdict(), sys.stdout, ensure_ascii=False)
